home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1996 #3 / AmigaPlus_CD-ROM-EXTRA_Nr.3.bin / aminet-spiele / role on / larn / tgoto.c < prev    next >
C/C++ Source or Header  |  1997-12-31  |  6KB  |  253 lines

  1. /************************************************************************
  2.  *                                  *
  3.  *          Copyright (c) 1982, Fred Fish           *
  4.  *              All Rights Reserved             *
  5.  *                                  *
  6.  *  This software and/or documentation is released for public   *
  7.  *  distribution for personal, non-commercial use only.     *
  8.  *  Limited rights to use, modify, and redistribute are hereby  *
  9.  *  granted for non-commercial purposes, provided that all      *
  10.  *  copyright notices remain intact and all changes are clearly *
  11.  *  documented.  The author makes no warranty of any kind with  *
  12.  *  respect to this product and explicitly disclaims any implied    *
  13.  *  warranties of merchantability or fitness for any particular *
  14.  *  purpose.                            *
  15.  *                                  *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *  tgoto   expand cursor addressing string from cm capability
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *  termcap
  28.  *
  29.  *  SYNOPSIS
  30.  *
  31.  *  char *tgoto(cm,destcol,destline)
  32.  *  char *cm;
  33.  *  int destcol;
  34.  *  int destline;
  35.  *
  36.  *  DESCRIPTION
  37.  *
  38.  *  Returns cursor addressing string, decoded from the cm
  39.  *  capability string, to move cursor to column destcol on
  40.  *  line destline.
  41.  *
  42.  *  The following sequences uses one input argument, either
  43.  *  line or column, and place the appropriate substitution
  44.  *  in the output string:
  45.  *
  46.  *      %d  substitute decimal value (in ASCII)
  47.  *      %2  like %d but forces field width to 2
  48.  *      %3  like %d but forces field width to 3
  49.  *      %.  like %c
  50.  *      %+x like %c but adds ASCII value of x
  51.  *
  52.  *  The following sequences cause processing modifications
  53.  *  but do not "use up" one of the arguments.  If they
  54.  *  act on an argument they act on the next one to
  55.  *  be converted.
  56.  *
  57.  *      %>xy    if next value to be converted is
  58.  *          greater than value of ASCII char x
  59.  *          then add value of ASCII char y.
  60.  *      %r  reverse substitution of line
  61.  *          and column (line is substituted
  62.  *          first by default).
  63.  *      %i  causes input values destcol and
  64.  *          destline to be incremented.
  65.  *      %%  gives single % character in output.
  66.  *
  67.  *  BUGS
  68.  *
  69.  *  Does not implement some of the more arcane sequences for
  70.  *  radically weird terminals (specifically %n, %B, & %D).
  71.  *  If you have one of these you deserve whatever happens.
  72.  *
  73.  */
  74.  
  75. /*
  76.  *  Miscellaneous stuff
  77.  */
  78.  
  79. #include <stdio.h>
  80.  
  81. #define MAXARGS 2
  82.  
  83. static char *in;        /* Internal copy of input string pointer */
  84. static char *out;       /* Pointer to output array */
  85. static int args[MAXARGS];   /* Maximum number of args to convert */
  86. static int pcount;      /* Count of args processed */
  87. static char output[64];     /* Converted string */
  88.  
  89.  
  90. /*
  91.  *  PSEUDO CODE
  92.  *
  93.  *  Begin tgoto
  94.  *      If no string to process then
  95.  *      Return pointer to error string.
  96.  *      Else
  97.  *      Initialize pointer to input string.
  98.  *      Initialize pointer to result string.
  99.  *      First arg is line number by default.
  100.  *      Second arg is col number by default.
  101.  *      No arguments processed yet.
  102.  *      While there is another character to process
  103.  *          If character is a not a % character then
  104.  *          Simply copy to output.
  105.  *          Else
  106.  *          Process the control sequence.
  107.  *          End if
  108.  *      End while
  109.  *      TERMINATE STRING!  (rde)
  110.  *      Return pointer to static output string.
  111.  *      End if
  112.  *  End tgoto
  113.  *
  114.  */
  115.  
  116. char *tgoto(cm,destcol,destline)
  117. char *cm;
  118. int destcol;
  119. int destline;
  120. {
  121.     if (cm == NULL) {
  122.     return("OOPS");
  123.     } else {
  124.     in = cm;
  125.     out = output;
  126.     args[0] = destline;
  127.     args[1] = destcol;
  128.     pcount = 0;
  129.     while (*in != NULL) {
  130.         if (*in != '%') {
  131.         *out++ = *in++;
  132.         } else {
  133.         process();
  134.         }
  135.     }
  136.     *out = '\0';    /* rde 18-DEC-86: don't assume out was all zeros */
  137.     return(output);
  138.     }
  139. }
  140.  
  141. /*
  142.  *  INTERNAL FUNCTION
  143.  *
  144.  *  process   process the conversion/command sequence
  145.  *
  146.  *  SYNOPSIS
  147.  *
  148.  *  static process()
  149.  *
  150.  *  DESCRIPTION
  151.  *
  152.  *  Processes the sequence beginning with the % character.
  153.  *  Directly manipulates the input string pointer, the
  154.  *  output string pointer, and the arguments.  Leaves
  155.  *  the input string pointer pointing to the next character
  156.  *  to be processed, and the output string pointer pointing
  157.  *  to the next output location.  If conversion of
  158.  *  one of the numeric arguments occurs, then the pcount
  159.  *  is incremented.
  160.  *
  161.  */
  162.  
  163. /*
  164.  *  PSEUDO CODE
  165.  *
  166.  *  Begin process
  167.  *      Skip over the % character.
  168.  *      Switch on next character after %
  169.  *      Case 'd':
  170.  *      Process %d type conversion (variable width).
  171.  *      Reinitialize output pointer.
  172.  *      Break;
  173.  *      Case '2':
  174.  *      Process %d type conversion (width 2).
  175.  *      Reinitialize output pointer.
  176.  *      Break;
  177.  *      Case '3':
  178.  *      Process %d type conversion (width 3).
  179.  *      Reinitialize output pointer.
  180.  *      Break;
  181.  *      Case '.'
  182.  *      Process %c type conversion.
  183.  *      Break;
  184.  *      Case '+':
  185.  *      Process %c type conversion with offset.
  186.  *      Break;
  187.  *      Case '>':
  188.  *      Process argument modification.
  189.  *      Break;
  190.  *      Case 'r':
  191.  *      Process argument reversal.
  192.  *      Break;
  193.  *      Case 'i':
  194.  *      Increment argument values.
  195.  *      Break;
  196.  *      Case '%':
  197.  *      Copy to output, incrementing pointers.
  198.  *      Break;
  199.  *      End switch
  200.  *  End process
  201.  *
  202.  */
  203.  
  204.  
  205. static process()
  206. {
  207.     int temp;
  208.  
  209.     in++;
  210.     switch(*in++) {
  211.     case 'd':
  212.     sprintf(out,"%d",args[pcount++]);
  213.     out = &output[strlen(output)];  
  214.     break;
  215.     case '2':
  216.     sprintf(out,"%02d",args[pcount++]);
  217.     out += 2 ;
  218. /*
  219.     out = &output[strlen(output)];
  220. */
  221.     break;
  222.     case '3':
  223.     sprintf(out,"%03d",args[pcount++]);
  224.     out = &output[strlen(output)];
  225.     break;
  226.     case '.':
  227.     *out++ = args[pcount++];
  228.     break;
  229.     case '+':
  230.     *out++ = args[pcount++] + *in++;
  231.     break;
  232.     case '>':
  233.     if (args[pcount] > *in++) {
  234.         args[pcount] += *in++;
  235.     } else {
  236.         in++;
  237.     }
  238.     break;
  239.     case 'r':
  240.     temp = args[pcount];
  241.     args[pcount] = args[pcount+1];
  242.     args[pcount+1] = temp;
  243.     break;
  244.     case 'i':
  245.     args[pcount]++;
  246.     args[pcount+1]++;
  247.     break;
  248.     case '%':
  249.     *out++ = '%';
  250.     break;
  251.     }
  252. }
  253.